﻿using LyData.Data;
using System;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ChangeMyInformation : System.Web.UI.Page
{
    protected void Chanage_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            string[,] strArray;
            IDb db = new DbFactory().DriverDb();
            db.Open();
            if (((this.OldPassword.Text == "") | (this.Password.Text == "")) | (this.AffirmPassword.Text == ""))
            {
                strArray = new string[,] { { "PenName", this.PenName.Text.ToString(), "NVarChar", "32" }, { "UpTime", DateTime.Now.ToString(), "DateTime", "0" } };
            }
            else
            {
                strArray = new string[,] { { "PenName", this.PenName.Text.ToString(), "NVarChar", "32" }, { "UpTime", DateTime.Now.ToString(), "DateTime", "0" }, { "Password", Function.MD5(this.Password.Text.ToString()), "NVarChar", "32" } };
            }
            string whereStr = string.Format("ManagerName='{0}'", (string)this.Session["ManagerName"]);
            db.Update("manager", strArray, whereStr);
            HttpContext.Current.Session["penName"] = this.PenName.Text.ToString();
            this.ShowInfo();
            this.Msg.Text = "资料修改成功！";
            this.Msg.Visible = true;
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        string whereStr = string.Format("ManagerName='{0}' AND Password='{1}'", (string)this.Session["ManagerName"], Function.MD5(args.Value.ToString()));
        IDb db = new DbFactory().DriverDb();
        db.Open();
        int num = db.Scalar("count('ManagerName')", "manager", whereStr);
        db.Close();
        if (num > 0)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ManagerLogin.ValidateRole("B_ChangeMyInformation.aspx"))
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "您无此权限！";
            this.Msg.Visible = true;
            this.Chanage.Enabled = false;
        }
        else
        {
            this.Msg.Visible = false;
            if (!base.IsPostBack)
            {
                this.ShowInfo();
            }
        }
    }

    protected void ShowInfo()
    {
        string str = (string)this.Session["managerName"];
        if (str != null)
        {
            this.ManagerName.Text = str;
            this.PenName.Text = (string)this.Session["penName"];
        }
    }
}
